package order

import (
	"web/models"
)

// EncodeOrderService 订单编码服务
type EncodeOrderService struct {
}

// NewEncodeOrderService 创建编码服务实例
func NewEncodeOrderService() *EncodeOrderService {
	return &EncodeOrderService{}
}

// EncodeLint 将 LaOrder 编码为 RLintOrder
func (s *EncodeOrderService) EncodeLint(order *models.LaOrder) *RLintOrder {
	if order == nil {
		return nil
	}

	// 从填充服务获取订单产品
	fillService := NewFillOrderService()
	orderProjects := fillService.GetOrderProjects(uint(order.ID))

	// 编码订单产品
	orderItems := make([]*ROrderProject, 0, len(orderProjects))
	for _, item := range orderProjects {
		orderItems = append(orderItems, &ROrderProject{
			ID:        item.ID,
			OrderID:   item.OrderID,
			OrderCode: item.OrderCode,
			ProjectID: item.ProjectID,
			Project:   item.Project,
			Price:     item.Price,
			Duration:  item.Duration,
			Count:     item.Count,
		})
	}

	// 从填充服务获取订单评价
	orderEvaluates := fillService.GetOrderEvaluates(uint64(order.ID))

	// 编码订单评价
	evaluates := make([]*ROrderEvaluate, 0, len(orderEvaluates))
	for _, evaluate := range orderEvaluates {
		evaluates = append(evaluates, &ROrderEvaluate{
			ID:      evaluate.ID,
			OrderID: evaluate.OrderID,
			Uid:     evaluate.Uid,
			Star:    evaluate.Star,
			Content: evaluate.Content,
			Tag:     evaluate.Tag,
			IsAnony: evaluate.IsAnony,
		})
	}

	return &RLintOrder{
		ID:            order.ID,
		OrderNo:       order.OrderNo,
		Status:        order.Status,
		StatusText:    s.getStatusText(order.Status),
		OrderAmount:   order.OrderAmount,
		CreatedAt:     order.CreatedAt.Format("2006-01-02 15:04:05"),
		OrderItems:    orderItems,
		OrderEvaluate: evaluates,
	}
}

// Encode 将 LaOrder 编码为 ROrder
func (s *EncodeOrderService) Encode(order *models.LaOrder) *ROrder {
	if order == nil {
		return nil
	}

	// 从填充服务获取订单产品
	fillService := NewFillOrderService()
	orderProjects := fillService.GetOrderProjects(uint(order.ID))

	// 编码订单产品
	orderItems := make([]*ROrderProject, 0, len(orderProjects))
	for _, item := range orderProjects {
		orderItems = append(orderItems, &ROrderProject{
			ID:        item.ID,
			OrderID:   item.OrderID,
			OrderCode: item.OrderCode,
			ProjectID: item.ProjectID,
			Project:   item.Project,
			Price:     item.Price,
			Duration:  item.Duration,
			Count:     item.Count,
		})
	}

	// 从填充服务获取订单评价
	orderEvaluates := fillService.GetOrderEvaluates(uint64(order.ID))

	// 编码订单评价
	evaluates := make([]*ROrderEvaluate, 0, len(orderEvaluates))
	for _, evaluate := range orderEvaluates {
		evaluates = append(evaluates, &ROrderEvaluate{
			ID:      evaluate.ID,
			OrderID: evaluate.OrderID,
			Uid:     evaluate.Uid,
			Star:    evaluate.Star,
			Content: evaluate.Content,
			Tag:     evaluate.Tag,
			IsAnony: evaluate.IsAnony,
		})
	}

	return &ROrder{
		ID:             order.ID,
		Uid:            order.Uid,
		ServiceTime:    order.ServiceTime,
		Sn:             order.Sn,
		OrderNo:        order.OrderNo,
		UserCouponID:   order.UserCouponID,
		Coupon:         order.Coupon,
		Address:        order.Address,
		Status:         order.Status,
		StatusText:     s.getStatusText(order.Status),
		Remark:         order.Remark,
		PayType:        order.PayType,
		PayTrade:       order.PayTrade,
		PayStatus:      order.PayStatus,
		OrderAmount:    order.OrderAmount,
		PayAmount:      order.PayAmount,
		PayRemark:      order.PayRemark,
		IsAppraise:     order.IsAppraise,
		CancelRemark:   order.CancelRemark,
		CancelDatetime: order.CancelDatetime,
		CreatedAt:      order.CreatedAt.Format("2006-01-02 15:04:05"),
		UpdatedAt:      order.UpdatedAt.Format("2006-01-02 15:04:05"),
		OrderItems:     orderItems,
		OrderEvaluate:  evaluates,
	}
}

// EncodeList 编码订单列表
func (s *EncodeOrderService) EncodeList(orders []*models.LaOrder) []*RLintOrder {
	result := make([]*RLintOrder, 0, len(orders))
	for _, order := range orders {
		result = append(result, s.EncodeLint(order))
	}
	return result
}

// getStatusText 获取状态文本
func (s *EncodeOrderService) getStatusText(status int) string {
	if text, ok := models.OrderStatusDescription[int8(status)]; ok {
		return text
	}
	return "未知状态"
}